﻿Imports TMySQL
Public Class frmCreateUser
    Public Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub

    Private Sub InitFrm()
        frmMain.GeneralSettings.ApplyAll(Me)
        txtUserName.Text = String.Empty
        txtHostName.Text = String.Empty
        txtPassword1.Text = String.Empty
        txtPassword2.Text = String.Empty

        chkSelect.Checked = False
        chkInsert.Checked = False
        chkUpdate.Checked = False
        chkDelete.Checked = False
        chkFile.Checked = False

        chkCreate.Checked = False
        chkAlter.Checked = False
        chkIndex.Checked = False
        chkDrop.Checked = False
        chkCreateTemporaryTables.Checked = False
        chkShowView.Checked = False
        chkCreateRoutine.Checked = False
        chkAlterRoutine.Checked = False
        chkExecute.Checked = False
        chkCreateView.Checked = False
        chkEvent.Checked = False
        chkTrigger.Checked = False

        chkGrant.Checked = False
        chkSuper.Checked = False
        chkProcess.Checked = False
        chkReload.Checked = False
        chkShutdown.Checked = False
        chkShowDatabase.Checked = False
        chkLockTables.Checked = False
        chkReferences.Checked = False
        chkReplClient.Checked = False
        chkReplSlave.Checked = False
        chkCreateUser.Checked = False

        txtMaxConnections.Text = "0"
        txtMaxQueries.Text = "0"
        txtMaxUpdates.Text = "0"
        txtMaxUserConnections.Text = "0"
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.Close()
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Dim myQuery As New TMyQuery
        Try
            myQuery.Session = frmMain.Connection
            If MsgBox("この内容でユーザーを作成しますか？", MsgBoxStyle.Question Or MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                Dim userName As String = txtUserName.Text
                Dim hostName As String = txtHostName.Text
                CreateUser(myQuery, userName, hostName, txtPassword1.Text)
                AddPrivs(myQuery, userName, hostName)
                Call MsgBox("ユーザーを作成しました", MsgBoxStyle.Information)
                Me.Close()
            End If
        Catch ex As Exception
            Call MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        Finally
            myQuery.Dispose()
        End Try
    End Sub

    Private Sub CreateUser(ByRef AQuery As TMyQuery, ByVal AUser As String, ByVal AHost As String, ByVal APassword As String)
        AQuery.Clear()
        AQuery.SQL.Append("create user '").Append(AUser).Append("'@'").Append(AHost).Append("' identified by '").Append(APassword).Append("'")
        AQuery.Execute()
    End Sub
    Private Sub AddPrivs(ByRef AQuery As TMyQuery, ByVal AUser As String, ByVal AHost As String)

        Dim sb As New System.Text.StringBuilder
        Dim sbSQL As New System.Text.StringBuilder
        sbSQL.AppendLine("GRANT ")

        If chkSelect.Checked Then sb.AppendLine("SELECT")
        If chkInsert.Checked Then sb.AppendLine("INSERT")
        If chkUpdate.Checked Then sb.AppendLine("UPDATE")
        If chkDelete.Checked Then sb.AppendLine("DELETE")
        If chkFile.Checked Then sb.AppendLine("FILE")

        If chkCreate.Checked Then sb.AppendLine("CREATE")
        If chkAlter.Checked Then sb.AppendLine("ALTER")
        If chkIndex.Checked Then sb.AppendLine("INDEX")
        If chkDrop.Checked Then sb.AppendLine("DROP")
        If chkCreateTemporaryTables.Checked Then sb.AppendLine("CREATE TEMPORARY TABLES")
        If chkShowView.Checked Then sb.AppendLine("SHOW VIEW")
        If chkCreateRoutine.Checked Then sb.AppendLine("CREATE ROUTINE")
        If chkAlterRoutine.Checked Then sb.AppendLine("ALTER ROUTINE")
        If chkExecute.Checked Then sb.AppendLine("EXECUTE")
        If chkCreateView.Checked Then sb.AppendLine("CREATE VIEW")
        If chkEvent.Checked Then sb.AppendLine("EVENT")
        If chkTrigger.Checked Then sb.AppendLine("TRIGGER")

        If chkSuper.Checked Then sb.AppendLine("SUPER")
        If chkProcess.Checked Then sb.AppendLine("PROCESS")
        If chkReload.Checked Then sb.AppendLine("RELOAD")
        If chkShutdown.Checked Then sb.AppendLine("SHUTDOWN")
        If chkShowDatabase.Checked Then sb.AppendLine("SHOW DATABASES")
        If chkLockTables.Checked Then sb.AppendLine("LOCK TABLES")
        If chkReferences.Checked Then sb.AppendLine("REFERENCES")
        If chkReplClient.Checked Then sb.AppendLine("REPLICATION CLIENT")
        If chkReplSlave.Checked Then sb.AppendLine("REPLICATION SLAVE")
        If chkCreateUser.Checked Then sb.AppendLine("CREATE USER")
        Dim sTmp As String = sb.ToString.Replace(vbNewLine, ",")
        If sTmp.Length > 0 Then
            sTmp = Common.Left(sTmp, sTmp.Length - 1)
        End If
        sbSQL.AppendLine(sTmp)
        sbSQL.AppendLine(" ON *.*")
        If chkGrant.Checked Then
            sbSQL.AppendLine("WITH GRANT OPTION")
        End If
        sbSQL.Append("TO '").Append(AUser).Append("'@'").Append(AHost).AppendLine("'")
        sbSQL.Append("MAX_QUERIES_PER_HOUR ").AppendLine(Common.StrToIntDef(txtMaxQueries.Text, 0))
        sbSQL.Append("MAX_CONNECTIONS_PER_HOUR ").AppendLine(Common.StrToIntDef(txtMaxConnections.Text, 0))
        sbSQL.Append("MAX_UPDATES_PER_HOUR ").AppendLine(Common.StrToIntDef(txtMaxUpdates.Text, 0))
        sbSQL.Append("MAX_USER_CONNECTIONS ").AppendLine(Common.StrToIntDef(txtMaxUserConnections.Text, 0))
        AQuery.Clear()
        AQuery.SQL.Append(sbSQL.ToString)
        AQuery.Execute()
    End Sub

    Private Sub txt_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMaxUserConnections.KeyPress, txtMaxUpdates.KeyPress, txtMaxQueries.KeyPress, txtMaxConnections.KeyPress
        If e.KeyChar < "0"c OrElse e.KeyChar > "9"c Then
            e.Handled = True
        End If
    End Sub

    Private Sub cmdCheckOnAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheckOnAll.Click
        AllCheck(True)
    End Sub

    Private Sub cmdCheckOffAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheckOffAll.Click
        AllCheck(False)
    End Sub

    Private Sub AllCheck(ByVal AChecked As Boolean)
        chkSelect.Checked = AChecked
        chkInsert.Checked = AChecked
        chkUpdate.Checked = AChecked
        chkDelete.Checked = AChecked
        chkFile.Checked = AChecked

        chkCreate.Checked = AChecked
        chkAlter.Checked = AChecked
        chkIndex.Checked = AChecked
        chkDrop.Checked = AChecked
        chkCreateTemporaryTables.Checked = AChecked
        chkShowView.Checked = AChecked
        chkCreateRoutine.Checked = AChecked
        chkAlterRoutine.Checked = AChecked
        chkExecute.Checked = AChecked
        chkCreateView.Checked = AChecked
        chkEvent.Checked = AChecked
        chkTrigger.Checked = AChecked

        chkSuper.Checked = AChecked
        chkProcess.Checked = AChecked
        chkReload.Checked = AChecked
        chkShutdown.Checked = AChecked
        chkShowDatabase.Checked = AChecked
        chkLockTables.Checked = AChecked
        chkReferences.Checked = AChecked
        chkReplClient.Checked = AChecked
        chkReplSlave.Checked = AChecked
        chkCreateUser.Checked = AChecked
    End Sub
End Class